package com.nciae.chain.handler.security;

import com.nciae.po.Dormitory;
import com.nciae.po.DormitorySecurityQuestion;
import com.nciae.po.SecurityQuestion;
import com.nciae.service.IDormitorySecurityQuestionService;
import com.nciae.service.IDormitoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Order(3)
public class DormitorySecurityQuestionHandler implements QHandler {

    @Autowired
    private IDormitorySecurityQuestionService dormitorySecurityQuestionService;

    @Autowired
    private IDormitoryService dormitoryService;

    @Override
    public void service(SecurityQuestion securityQuestion, int option) {

        if (option == 1) {
            DormitorySecurityQuestion dormitorySecurityQuestion = new DormitorySecurityQuestion();
            dormitorySecurityQuestion.setDormitoryId(securityQuestion.getDormitory().getId());
            dormitorySecurityQuestion.setSecurityQuestionId(securityQuestion.getId());
            dormitorySecurityQuestionService.add(dormitorySecurityQuestion);
        }

        if (option == 2) {
            List<DormitorySecurityQuestion> dormitorySecurityQuestions = dormitorySecurityQuestionService.findBySecurityQuestionId(securityQuestion.getId());
            for(DormitorySecurityQuestion dormitorySecurityQuestion : dormitorySecurityQuestions) {
                Dormitory dormitory = dormitoryService.findDormitoryById(dormitorySecurityQuestion.getDormitoryId());
                securityQuestion.setDormitory(dormitory);
            }
        }
    }


}
